﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.Validation
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class RangeValidatorAttribute : ValidatorAttribute
    {
        
        private RangeValidatorAttribute(IComparable lowerBound, IComparable upperBound)
            : base(lowerBound, upperBound)
        {

        }

        public RangeValidatorAttribute(int lowerBound, int upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {

        }

        public RangeValidatorAttribute(double lowerBound, double upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {

        }

        public RangeValidatorAttribute(decimal lowerBound, decimal upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {

        }

        public RangeValidatorAttribute(float lowerBound, float upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {

        }

        public RangeValidatorAttribute(short lowerBound, short upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {

        }

        public RangeValidatorAttribute(string lowerBound, string upperBound)
            : this((IComparable)lowerBound, (IComparable)upperBound)
        {
        }

        public RangeValidatorAttribute(string lowerBound, string upperBound, Type type)
            : this(
            (IComparable)Dycox.Utils.ConvertUtil.ConvertType(lowerBound, type),
            (IComparable)Dycox.Utils.ConvertUtil.ConvertType(upperBound, type)
            )
            
        {
        }
        
        private bool _excludeBoundValues;

        public bool ExcludeBoundValues
        {
            get { return _excludeBoundValues; }
            set { _excludeBoundValues = value; }
        }

        protected override Validator CreateValidator(object[] parameters, ITypeDescriptorContext context)
        {
            RangeValidator validator = new RangeValidator();
            validator.LowerBound = Dycox.Utils.ConvertUtil.ConvertType(parameters[0], context);
            validator.UpperBound = Dycox.Utils.ConvertUtil.ConvertType(parameters[1], context);
            validator.ExcludeBoundValues = _excludeBoundValues;
            return validator;
        }
    }
}
